package controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import entity.Dev_user;
import service.Backend_userloginService;

@Controller
@RequestMapping("/Receptionuser")
public class Backend_userlogin {
	
	@Autowired
	private Backend_userloginService backService;
	
	
	@RequestMapping("/Backlogin")
	public String login(HttpServletRequest request,HttpServletResponse response) throws Exception {
		String devCode = request.getParameter("devCode");
		String devPassword = request.getParameter("devPassword");
		Dev_user devuser =(Dev_user) request.getSession().getAttribute("devUserSession");
		if(devuser != null) {
			return "developer/main";
		}
		Dev_user dev_user = new Dev_user();
		dev_user.setDevCode(devCode);
		dev_user.setDevPassword(devPassword);
		dev_user = backService.query(devCode);
		if (dev_user == null) {
			request.setAttribute("error", "用户名错误,请重试");
			return "devlogin";
		}else {
			dev_user = backService.login(devCode, devPassword);
			if (dev_user == null) {
				request.setAttribute("error", "密码错误，请重试");
				return "devlogin";
			}else {
				request.getSession().setAttribute("devUserSession", dev_user);
			}
		}
		return "redirect:/Receptionuser/jlogin";
	}
	
	/**
	 * 系统退出
	 * @param request
	 * @return
	 */
	@RequestMapping("/logout")
	public String logout(HttpServletRequest request) {
		request.getSession().removeAttribute("devUserSession");
		return "devlogin";
	}
	
	/**
	 * 登录拦截
	 */
	@RequestMapping("jlogin")
	public String jlogin(HttpSession session) {

		return "developer/main";

	}
}
